package contact

import (
	"common/ws_contact"
	"context"
	"crypto/md5"
	"enode/db"
	"enode/store"
	"fmt"
	"github.com/google/uuid"
)

//接收处理center发过来的消息

type Endpoint struct {
	db    *db.DB
	store store.Store
}

func NewEndpoint(db *db.DB, store store.Store) *Endpoint {
	return &Endpoint{db: db, store: store}
}

func (ed *Endpoint) Ping(ctx context.Context) (err error) {
	fmt.Println("收到ping消息")
	return
}

func (ed *Endpoint) PieceDelete(ctx context.Context, req *ws_contact.PieceDeleteRequest) error {
	fmt.Println("收到删除数据消息：", req)
	md5Value := md5.Sum([]byte(req.Key))
	pieceId, _ := uuid.FromBytes(md5Value[:])
	return ed.store.DelPiece(ctx, pieceId)
}
